<template>
  <div class="manager-container">
    <!--  头部  -->
    <div class="manager-header">
      <div class="manager-header-left">
        <img src="@/assets/imgs/logo.png" />
        <div class="title">后台管理系统</div>
      </div>

      <div class="manager-header-center">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="manager-header-right">
        <el-dropdown placement="bottom">
          <div class="avatar">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <div>{{ user.name ||  '管理员' }}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goToPerson">个人信息</el-dropdown-item>
            <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <!--  主体  -->
    <div class="manager-main">
      <!--  侧边栏  -->
      <div class="manager-main-left">
        <el-menu :default-openeds="openeds" router style="border: none" :default-active="$route.path">
          <el-menu-item index="/manager/home">
            <i class="el-icon-house"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>

          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-setting"></i>
              <span slot="title">系统管理</span>
            </template>
            <el-menu-item index="/manager/admin">
              <i class="el-icon-user"></i>
              <span slot="title">管理员信息</span>
            </el-menu-item>
            <el-menu-item index="/manager/notice">
              <i class="el-icon-bell"></i>
              <span slot="title">公告信息</span>
            </el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-user"></i>
              <span slot="title">个人中心</span>
            </template>
            <el-menu-item index="/manager/person">
              <i class="el-icon-s-custom"></i>
              <span slot="title">个人信息</span>
            </el-menu-item>
            <el-menu-item index="/manager/password">
              <i class="el-icon-key"></i>
              <span slot="title">修改密码</span>
            </el-menu-item>
          </el-submenu>

          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-mobile-phone"></i>
              <span>设备管理</span>
            </template>
            <el-menu-item index="/manager/phone">
              <i class="el-icon-mobile"></i>
              <span slot="title">手机管理</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </div>

      <!--  数据表格  -->
      <div class="manager-main-right">
        <router-view @update:user="updateUser" />
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "Manager",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      openeds: [], // 存储当前展开的菜单
      menuMap: {
        '/manager/admin': ['1'],
        '/manager/notice': ['1'],
        '/manager/person': ['2'],
        '/manager/password': ['2'],
        '/manager/phone': ['3']
      }
    }
  },
  created() {
    if (!this.user.id) {
      this.$router.push('/login')
    }
    // 初始化展开的菜单
    this.updateOpeneds()
  },
  watch: {
    // 监听路由变化，更新展开的菜单
    '$route.path': {
      handler: function() {
        this.updateOpeneds()
      },
      immediate: true
    }
  },
  methods: {
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    updateOpeneds() {
      // 根据当前路径更新展开的菜单
      const currentPath = this.$route.path
      if (this.menuMap[currentPath]) {
        this.openeds = this.menuMap[currentPath]
      }
    },
    goToPerson() {
      if (this.user.role === 'ADMIN') {
        this.$router.push('/adminPerson')
      }
    },
    logout() {
      localStorage.removeItem('xm-user')
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
@import "@/assets/css/manager.css";
</style>